{extend name='admin@main'}

{block name="content"}

{include file='store@banner/form_style'}

<form onsubmit="return false;" id="GoodsForm" data-auto="true" method="post" class='layui-form layui-card' autocomplete="off">

    <div class="layui-card-body think-box-shadow padding-left-40">

        <div class="layui-form-item">
            <span class="color-green label-required-prev">轮播图片</span>
            <table class="layui-table">
                <thead>
                <tr>
                    <th class="text-left">轮播图片</th>
                    <!--<th class="text-left">展示图片</th>-->
                </tr>
                <tr>
                    <td width="90px" class="text-left"><input name="img" type="hidden" value="{$vo.img|default=''}"></td>
                    <!--<td width="auto" class="text-left"><input name="image" type="hidden" value="{$vo.image|default=''}"></td>-->
                </tr>
                </thead>
            </table>
            <script>$('[name="img"]').uploadOneImage()</script>
        </div>

        <div class="layui-form-item block">
            <span class="label-required-prev color-green">是否跳转</span>
            {if isset($vo.is_jump)}
            <input type="radio" name="is_jump" value="0" title="否" lay-filter="is_jump" {if $vo.is_jump eq 0}checked{/if}>
            <input type="radio" name="is_jump" value="1" title="是" lay-filter="is_jump" {if $vo.is_jump eq 1}checked{/if}>
            {else}
            <input type="radio" name="is_jump" value="0" title="否" lay-filter="is_jump" checked>
            <input type="radio" name="is_jump" value="1" title="是" lay-filter="is_jump">
            {/if}
        </div>

        <div class="layui-form-item block">
            <span class="label-required-prev color-green">类型</span>
            {if isset($vo.type)}
            <input type="radio" name="type" value="0" title="小程序端" lay-filter="type" {if $vo.type eq 0}checked{/if}>
            <input type="radio" name="type" value="1" title="PC端" lay-filter="type" {if $vo.type eq 1}checked{/if}>
            {else}
            <input type="radio" name="type" value="0" title="小程序端" lay-filter="type" checked>
            <input type="radio" name="type" value="1" title="PC端" lay-filter="type">
            {/if}
        </div>


        <div class="layui-form-item text-center">
            <input type="hidden" name="isAddMode" value="{$isAddMode}">
            {notempty name='vo.id'}<input type="hidden" name="id" value="{$vo.id}">{/notempty}
            <button class="layui-btn" type="submit">保存轮播图</button>
            <!--<button class="layui-btn layui-btn-danger" ng-click="hsitoryBack()" type="button">取消编辑</button>-->
        </div>

    </div>
</form>
{/block}

{block name='script'}
<textarea class="layui-hide" id="goods-specs">{$vo.specs|raw|default=''}</textarea>
<textarea class="layui-hide" id="goods-value">{$defaultValues|raw|default=''}</textarea>
<script>
    window.form.render();
    require(['ckeditor', 'angular'], function () {
        window.createEditor('[name="content"]', {height: 500});
        window.createEditor('[name="remarks"]', {height: 500});
        var app = angular.module("GoodsForm", []).run(callback);
        angular.bootstrap(document.getElementById(app.name), [app.name]);

        function callback($rootScope) {
            $rootScope.isAddMode = 1;
            $rootScope.maps = JSON.parse(angular.element('#goods-value').val() || '[]') || {};
            $rootScope.specs = JSON.parse(angular.element('#goods-specs').val() || '[{"name":"车队列表","list":[{"name":"头车","check":true,"cars":"1"}]}]');
            // 返回商品列表
            $rootScope.hsitoryBack = function () {
                $.msg.confirm('确定要取消编辑吗？', function (index) {
                    history.back(), $.msg.close(index);
                });
            };
            // 设置默认值
            $rootScope.setValue = function (key, type, value, call) {
                $rootScope.maps[key] || ($rootScope.maps[key] = {});
                return $rootScope.maps[key][type] = eval(call.replace('_', "'" + value + "'"));
            };
            // 读取默认值
            $rootScope.getValue = function (key, callback) {
                if (typeof callback === 'function') {
                    return callback($rootScope.maps[key] || {});
                }
                return {};
            };
            // 生成交叉表格数据
            $rootScope.specsTreeData = [];
            $rootScope.specsTreeNava = [];
            // 当前商品规格发生变化时重新计算规格列表
            $rootScope.$watch('specs', function () {
                var data = $rootScope.specs, list = [], navs = [], table = [[]];
                // 过滤无效记录
                for (var i in data) {
                    var tmp = [];
                    for (var j in data[i].list) if (data[i].list[j].check && data[i].list[j].name.length > 0) {
                        data[i].list[j].span = 1, data[i].list[j].show = true, data[i].list[j].group = data[i].name;
                        tmp.push(data[i].list[j]);
                    }
                    list.push(tmp), navs.push(data[i].name);
                }
                $rootScope.specsTreeNava = navs;
                // 表格交叉
                for (var i in list) {
                    var tmp = [];
                    for (var j in table) for (var k in list[i]) tmp.push(table[j].concat(list[i][k]));
                    table = tmp;
                }
                // 表格合并
                list = angular.fromJson(angular.toJson(table));
                for (var i in list) {
                    var key = [], _key = '';
                    for (var td in list[i]) key.push(list[i][td].group + '::' + list[i][td].name);
                    for (var td in list[i]) if (_key.length === 0) {
                        list[i][0].key = _key = key.join(';;');
                        list[i][0].car_id = $rootScope.getValue(_key, function (data) {
                            return data.car_id || '1';
                        });
                    }
                }
                console.log(list)
                $rootScope.specsTreeData = list;
            }, true);
            // 判断规则是否能取消选择
            $rootScope.checkListChecked = function (list, check) {
                for (var i in list) if (list[i].check) return check;
                return true;
            };

            // 增加分组的属性
            $rootScope.addSpecVal = function (list) {
                list.push({name: '跟车', check: true, cars: "0"});
            };
            // 移除分组的属性
            $rootScope.delSpecVal = function (data, $index) {
                var temp = [];
                for (var i in data) if (parseInt(i) !== parseInt($index)) temp.push(data[i]);
                return temp;
            };
        }
    })
</script>
<script>
     var isteam="{$vo.is_team|default='0'}";
     let adsss_url="{:url('/api/index/GetCityDis')}";
    $(function () {
        apply(isteam)
        window.form.render();
        window.form.on('radio(is_team)', function (data) {
            apply(data.value);
        });
        function apply(value) {
            if(value==0){
                 $(".car_team").hide();
                 $(".isshow").show();
            }else {
                $(".car_team").show();
                $(".isshow").hide()
            }
        }
        //车辆品牌选择
        window.form.on('select(brand_id)', function (data) {
            $.ajax({
                url : "{:url('goods/getCarModel')}",
                data :{'id':data.value},
                type : "post",
                success : function (e) {
                    layer.closeAll('loading');
                    console.log(e);
                    if (e.errcode == 200) {
                        layer.closeAll('loading');
                        var sel = document.getElementById("model_id");
                        $("#model_id").find("option").remove();
                        var edata=e.data
                        for(var i = 0;i < edata.length; i++) {
                            var opt = new Option();
                            opt.value= edata[i].id;
                            opt.text = edata[i].name;
                            sel.append(opt);
                        }
                        window.form.render();
                        // layer.msg('查询成功!',{icon:6});

                    }else{
                        layer.msg(e.msg,{icon:5});
                    }
                },
                error : function () {
                    layer.closeAll('loading');
                    layer.msg('服务器繁忙,请稍后再试!',{icon:5});
                }
            })
        });

        //省选择
        // window.form.on('select(province)', function (data) {
        //         addressChoose(adsss_url,data.value,'city');
        // });
        //市选择
        window.form.on('select(city)', function (data) {
            DistrictsChoose(adsss_url,data.value,'area');
        });



    });

</script>
{/block}